﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using HeThongThongTin.Library;
using Converter;
using System.Resources;
using System.Reflection;
using HeThongThongTin.UI.SL.HeThong.HT_DangKy;
using HeThongThongTin.UI.SL.HeThong.HT_QuanTriTaiKhoan;
using HeThongThongTin.UI.SL.HeThong.HT_VaiTro;


namespace HeThongThongTin.UI.SL.HeThong.HT_QuanLyVaiTroNguoiDung
{
    public partial class HT_QuanLyVaiTroNguoiDung_MainWindow : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "QuanLyVaiTroNguoiDung"; }
        }

        #endregion

        #region Load Methods
        List<HT_NguoiDung_VaiTro_ViewModel> NewListViewModel = new List<HT_NguoiDung_VaiTro_ViewModel>();
        string VaiTroNeedInsert;
        public HT_QuanLyVaiTroNguoiDung_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("HeThongThongTin.UI.SL.HeThong.HT_QuanLyVaiTroNguoiDung.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.HT_QuanLyVaiTroNguoiDung_GeneralInformation_GroupBox, controls);
            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource ht_NguoiDung_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_NguoiDung_ListViewModelSource"];
                HT_NguoiDung_ListViewModel ht_NguoiDung_ListViewModel = new HT_NguoiDung_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                ht_NguoiDung_ListViewSource.Source = new List<object> { ht_NguoiDung_ListViewModel };

                System.Windows.Data.CollectionViewSource ht_VaiTro_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_VaiTro_ListViewModelSource"];
                HT_VaiTro_ListViewModel ht_VaiTro_ListViewModel = new HT_VaiTro_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                ht_VaiTro_ListViewSource.Source = new List<object> { ht_VaiTro_ListViewModel };
                //ht_NguoiDung_ViewModel.Saved += new EventHandler(ht_NguoiDung_ViewModel_Saved);
              
            }
        }       
        #endregion

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        #region View Model Related Methods

        void HT_QuanLyVaiTroNguoiDung_ViewModel_Saved(object sender, EventArgs e)
        {
            //System.Windows.Data.CollectionViewSource HT_QuanLyVaiTroNguoiDung_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_QuanLyVaiTroNguoiDung_ListViewModelSource"];
            //HT_QuanLyVaiTroNguoiDung_ListViewModel vm = ((List<object>)HT_QuanLyVaiTroNguoiDung_ListViewSource.Source)[0] as HT_QuanLyVaiTroNguoiDung_ListViewModel;
            //vm.GetHT_QuanLyVaiTroNguoiDung_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
            
        }

       
        #endregion

        private void SaveItemButton_Click(object sender, RoutedEventArgs e)
        {
            
            int KhoaNguoiDung = ((HT_NguoiDungInfo)this.HT_QuanLyVaiTroNguoiDung_GeneralInformation_NguoiDungId_ComboBox.SelectedItem).Khoa;
            string ListVaitroCheck = "";
            string ListVaitroUncheck = "";

            foreach (HT_VaiTroInfo VaiTroItem in this.HT_QuanLyVaiTroNguoiDung_GeneralInformation_VaitroId_ItemsControl.Items)
            {
                var container = HT_QuanLyVaiTroNguoiDung_GeneralInformation_VaitroId_ItemsControl.ItemContainerGenerator.ContainerFromItem(VaiTroItem) as FrameworkElement;
                var checkBox = container.FindChildByType<CheckBox>() as CheckBox;
                if (checkBox.IsChecked == true)
                {
                    if (ListVaitroCheck.Equals(""))
                    {
                        ListVaitroCheck += VaiTroItem.Khoa;
                    }
                    else
                    {
                        ListVaitroCheck += ";" + VaiTroItem.Khoa;
                    }
                }
                else
                {
                    if (ListVaitroUncheck.Equals(""))
                    {
                        ListVaitroUncheck += VaiTroItem.Khoa;
                    }
                    else
                    {
                        ListVaitroUncheck += ";" + VaiTroItem.Khoa;
                    }
                }
            }

            System.Windows.Data.CollectionViewSource ht_NguoiDung_VaiTro_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_NguoiDung_VaiTro_ListViewModelSource"];
            HT_NguoiDung_VaiTro_ListViewModel ht_NguoiDung_VaiTro_ListViewModel = ((List<object>)ht_NguoiDung_VaiTro_ListViewSource.Source)[0] as HT_NguoiDung_VaiTro_ListViewModel;
            IList<HT_NguoiDung_VaiTroInfo> listNguoiDungVaiTro = ht_NguoiDung_VaiTro_ListViewModel.Model.getListOfInfo();

            
            
            foreach (HT_NguoiDung_VaiTroInfo item in listNguoiDungVaiTro)
            {
                string[] VaiTroUnCheckArray = ListVaitroUncheck.Split(';');
                
                if (VaiTroUnCheckArray.Contains(item.VaiTroId.ToString()))
                {
                     HeThongThongTin.Library.HT_NguoiDung_VaiTro.DeleteHT_NguoiDung_VaiTro(item.Khoa, (o, ex) =>
                    { 
                    });
                } 

            }

            VaiTroNeedInsert = KhoaNguoiDung.ToString();
            string[] VaiTroCheckArray = ListVaitroCheck.Split(';');
            for (int i = 0; i < VaiTroCheckArray.Count(); i++)
            {
                bool NeedInsert = true;
                foreach (HT_NguoiDung_VaiTroInfo item in listNguoiDungVaiTro)
                {
                    if (item.VaiTroId.ToString().Equals(VaiTroCheckArray[i]))
                    {
                        NeedInsert = false;
                        break;
                    }
                }
                if (NeedInsert)
                {
                    VaiTroNeedInsert += ";" + VaiTroCheckArray[i];     
                }
              
            }
           
            string[] VaiTroNeedInsertArray = VaiTroNeedInsert.Split(';');
            
            for(int i = 1 ; i < VaiTroNeedInsertArray.Count();i++)
            {
                System.Windows.Data.CollectionViewSource ht_NguoiDung_VaiTro_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_NguoiDung_VaiTro_ViewModelSource"];
                HT_NguoiDung_VaiTro_ViewModel ht_NguoiDung_VaiTro_ViewModel = new HT_NguoiDung_VaiTro_ViewModel();
                ht_NguoiDung_VaiTro_ViewModel.LoadCompleted +=new EventHandler(ht_NguoiDung_VaiTro_ViewModel_LoadCompleted);
                ht_NguoiDung_VaiTro_ViewSource.Source = new List<object> { ht_NguoiDung_VaiTro_ViewModel };
                NewListViewModel.Add(ht_NguoiDung_VaiTro_ViewModel);
                
            }

        }

        private bool AllLoadCompleted()
        {
            bool result = true;
            foreach (HT_NguoiDung_VaiTro_ViewModel item in NewListViewModel)
            {
                if (item.Model == null)
                {
                    result = false;
                }
            }
            return result;
        }
        private void ht_NguoiDung_VaiTro_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            string[] VaiTroNeedInsertArray = VaiTroNeedInsert.Split(';');

            if (AllLoadCompleted())
            {
                int index = 1;
                foreach (HT_NguoiDung_VaiTro_ViewModel item in NewListViewModel)
                {
                    item.Model.NguoiDungId = int.Parse(VaiTroNeedInsertArray[0]);
                    item.Model.VaiTroId = int.Parse(VaiTroNeedInsertArray[index]);
                    index++;
                    item.Save(this,new Csla.Xaml.ExecuteEventArgs());
                }

                while (NewListViewModel.Count != 0)
                {
                    NewListViewModel.RemoveAt(0);
                }
                VaiTroNeedInsert = "";
            }
        }
        private void HT_QuanLyVaiTroNguoiDung_GeneralInformation_NguoiDungId_ComboBox_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (HT_QuanLyVaiTroNguoiDung_GeneralInformation_NguoiDungId_ComboBox.SelectedItem != null)
            {
                System.Windows.Data.CollectionViewSource ht_NguoiDung_VaiTro_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_NguoiDung_VaiTro_ListViewModelSource"];
                HT_NguoiDung_VaiTro_ListViewModel ht_NguoiDung_VaiTro_ListViewModel = new HT_NguoiDung_VaiTro_ListViewModel(((HT_NguoiDungInfo)this.HT_QuanLyVaiTroNguoiDung_GeneralInformation_NguoiDungId_ComboBox.SelectedItem).Khoa);
                ht_NguoiDung_VaiTro_ListViewSource.Source = new List<object> { ht_NguoiDung_VaiTro_ListViewModel };
                ht_NguoiDung_VaiTro_ListViewModel.LoadCompleted += new EventHandler(ht_NguoiDung_VaiTro_ListViewModel_LoadCompleted);

            }
        }

        private void ht_NguoiDung_VaiTro_ListViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource ht_NguoiDung_VaiTro_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_NguoiDung_VaiTro_ListViewModelSource"];
            HT_NguoiDung_VaiTro_ListViewModel vm = ((List<object>)ht_NguoiDung_VaiTro_ListViewSource.Source)[0] as HT_NguoiDung_VaiTro_ListViewModel;
            IList<HT_NguoiDung_VaiTroInfo> listNguoiDungVaiTro = vm.Model.getListOfInfo();
           
               
            foreach (HT_VaiTroInfo VaiTroItem in this.HT_QuanLyVaiTroNguoiDung_GeneralInformation_VaitroId_ItemsControl.Items)
            {
                bool IsSelect = false;
                foreach (HT_NguoiDung_VaiTroInfo item in listNguoiDungVaiTro)
                {
                    if (item.VaiTroId == VaiTroItem.Khoa)
                    {
                        var container = HT_QuanLyVaiTroNguoiDung_GeneralInformation_VaitroId_ItemsControl.ItemContainerGenerator.ContainerFromItem(VaiTroItem) as FrameworkElement;
                        var checkBox = container.FindChildByType<CheckBox>() as CheckBox;
                        checkBox.IsChecked = true;
                        IsSelect = true;
                        break;
                    }
                        
                }
                if(!IsSelect)
                {
                    var container = HT_QuanLyVaiTroNguoiDung_GeneralInformation_VaitroId_ItemsControl.ItemContainerGenerator.ContainerFromItem(VaiTroItem) as FrameworkElement;
                    var checkBox = container.FindChildByType<CheckBox>() as CheckBox;
                    checkBox.IsChecked = false;
                }
            }
            
        }
    }
}
